package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetMealDto;
import com.itheima.reggie.entity.SetMeal;
import com.itheima.reggie.entity.SetMealDish;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {
    @Autowired
    private SetMealDishService setMealDishService;
    @Autowired
    private SetMealService setMealService;

    @Transactional
    public void saveWithDish(SetMealDto setMealDto) {
        //保存套餐的基本信息，操作setMeal，执行insert操作；
        this.save(setMealDto);

        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes();
        setMealDishes.stream().map((item) -> {
            item.setSetMealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setMeal_dish,执行insert操作
        setMealDishService.saveBatch(setMealDishes);
    }


    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(SetMeal::getId, ids);
        queryWrapper.eq(SetMeal::getStatus, 1);

        int count = this.count(queryWrapper);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetMealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetMealDish::getSetMealId, ids);
        //删除关系表中的数据----setmeal_dish
        setMealDishService.remove(lambdaQueryWrapper);
    }

    @Transactional
    public void statusWithDish(List<Long> ids, Integer status) {
        LambdaUpdateWrapper<SetMeal> qw = new LambdaUpdateWrapper<>();
        qw.set(SetMeal::getStatus, status);
        qw.in(SetMeal::getId, ids);
        setMealService.update(qw);

        for (Long id : ids) {
            SetMeal setMeal = new SetMeal();
            setMeal.setId(id);
            setMeal.setStatus(status);
            setMealService.updateById(setMeal);
        }
    }


    public SetMealDto getByIdWithFlavor(Long id) {
        //查询套餐基本信息，setMeal
        SetMeal setMeal = this.getById(id);
        SetMealDto setMealDto = new SetMealDto();
        BeanUtils.copyProperties(setMeal,setMealDto);

        //查询当前菜品对应的价格信息，从setMeal_dish表查询
        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMealDish::getSetMealId,setMeal.getId());
        setMealDto.setSetmealDishes(setMealDishService.list(queryWrapper));

        return setMealDto;
    }

    @Transactional
    public void updateWithFlavor(SetMealDto setMealDto) {
        //更新setMeal表基本信息
        this.updateById(setMealDto);

        //清理当前套餐对应菜单数据---setMeal_dish表的delete操作
        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetMealDish::getSetMealId,setMealDto.getId());

        setMealDishService.remove(queryWrapper);

        //添加当前提交过来的菜单数据---setMeal_dish表的insert操作
        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes();

        setMealDishes = setMealDishes.stream().map((item) -> {
            item.setSetMealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setMealDishService.saveBatch(setMealDishes);
    }
}

